#ifndef WINMACROCOMMANDLINE
#define WINMACROCOMMANDLINE

#include <string>
#include <ostream>
#include "OptionsPersister.h"

enum Action
{
	Record,
	Playback
};

class WinMacroCommandLine
{
	Action m_Action;
	std::string m_CommandLine;
	std::string m_File_name;
	std::stringstream str;

public:
	WinMacroCommandLine(
		 const std::string &command_line)
		: m_Action(Playback), str(std::stringstream(command_line)), m_CommandLine(command_line)
	{
	}

	Action GetAction() { return m_Action; }
	const std::string &GetFileName() { return m_File_name; }

private:

	int GetIntegerOption(const std::string &parameter, const std::string &option)
	{
		if (option.find(parameter + ":") == 0)
		{
			std::string number = option.substr(parameter.length() + 1);
			return atoi(number.c_str());
		}

		return -1;
	}

	std::string ReadString()
	{
		std::string result;
		std::string temp;

		if (!str.eof())
		{
			str >> result;

			if (result.at(0) != '"')
			{
				return result;
			}
			else if (result.at(result.length() - 1) == '"')
			{
				// remove first and last character to skip double quote
				return result.substr(1, result.length() - 2);
			}
			else
			{
				// Skip double quote
				result = result.substr(1);

				while (!str.eof())
				{
					str >> temp;

					result += " " + temp;

					if (temp.at(temp.length() - 1) == '"')
					{
						// Skip double quote
						result = result.substr(0, result.length() - 1);
						return result;
					}
				}
			}
		}

		return "";
	}
	
public:

	void ParseAndUpdateOptions(RECORDINGOPTIONS &recording_options, PLAYBACKOPTIONS &playback_options)
	{
		std::string option;

		m_File_name = ReadString();

		while (!str.eof())
		{
			str >> option;
			
			option = option.substr(1);

			int playback_time_interval = GetIntegerOption("playbacktimeinterval", option);
			if (playback_time_interval != -1)
			{
				playback_options.user_defined_time_interval = playback_time_interval;
			}

			int repeat_count = GetIntegerOption("repeat", option);
			if (repeat_count != -1)
			{
				playback_options.repeat_count = repeat_count;
			}

			if (option == "playback")
				m_Action = Playback;
			else if (option == "record")
				m_Action = Record;
			else if (option == "ignoremouse:yes" || option == "ignoremouse")
				recording_options.ignore_mouse_actions = true;
			else if (option == "ignoremouse:no")
				recording_options.ignore_mouse_actions = false;
			else if (option == "playbackatrecordedspeed:yes" || option == "playbackatrecordedspeed")
				playback_options.normal_speed = true;
			else if (option == "playbackatrecordedspeed:no")
				playback_options.normal_speed = false;
			else if (option == "addtorecordedtime:yes" || option == "addtorecordedtime")
				playback_options.add_to_recorded_time = true;
			else if (option == "addtorecordedtime:no")
				playback_options.add_to_recorded_time = false;
		}
	}
};

#endif